/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.cqq.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.cqq.model.CodeBase;
import com.yunhoo.cqq.model.Document;
import com.yunhoo.cqq.model.DocumentType;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author chen
 */
public class IbatisDocument {

    private final static String NAMESPACE = "document.";
    private static SqlMapClient sqlmapclient = IbatisTemplate.getSqlMapClient();

    public static void insertDocument(Document document) {//插入cf_document上传的文档
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.insert(NAMESPACE + "insertDocument", document);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误insertDocument~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void insertDocList(List<Document> docList) {
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.startBatch();
            for (Document document : docList) {
                sqlmapclient.insert(NAMESPACE + "insertDocument", document);
            }
            sqlmapclient.executeBatch();
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List getRootDocType() {  //获得cf_doctype表的根节点
        List<DocumentType> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE + "getRootDocType",new DocumentType());
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getRootDocType~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getSubDocType(DocumentType type) { //获得子节点
        List<DocumentType> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE + "getSubDocType", type);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getSubDocType~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static Integer getTopDocId() { //获得最近文档ID
        int id = 0;
        try {
            sqlmapclient.startTransaction();
            id = (Integer) sqlmapclient.queryForObject(NAMESPACE + "getTopDocId",new DocumentType());
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getTopDocId~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    public static String getTitleById(int id) { //根据ID获得文档名字
        String title = null;
        try {
            sqlmapclient.startTransaction();
            
            Map map = new HashMap();
            map.put("orgCode",LoginParam.getOrg().getCode());
            map.put("id",id);
            title = (String) sqlmapclient.queryForObject(NAMESPACE + "getTitleById", id);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getTitleById~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return title;
    }

    public static List getSelectDocType(String typeid) { //获得所选文档类型节点的所有文档
        List<Document> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            
            Map map = new HashMap();
            map.put("orgCode",LoginParam.getOrg().getCode());
            map.put("typeid",typeid);
            list = sqlmapclient.queryForList(NAMESPACE + "getSelectDocType", map);
            if (list != null && list.size() > 0) {
                for (Document doc : list) {
//                    String filepath1 = FacesUtil.getServletContext().getRealPath(NAMESPACE+"/");
//                    String filepath = filepath1 + doc.getUrl();
                    System.out.println(NAMESPACE + "filePath:" + doc.getUrl());
                    File file = new File(doc.getUrl());
                    if (file.exists()) {
                        doc.setShowflag(true);
                    } else {
                        doc.setShowflag(false);
                    }

                }
            }
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getSelectDocType~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static String getRulecontentById(String ruleid) { //获得文档类型的命名规则
        String rulecontent = null;
        try {
            sqlmapclient.startTransaction();
            
            Map map = new HashMap();
            map.put("orgCode",LoginParam.getOrg().getCode());
            map.put("ruleid",ruleid);
            rulecontent = (String) sqlmapclient.queryForObject(NAMESPACE + "getRulecontentById", map);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getRulecontentById~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rulecontent;
    }

    public static List checkDocRepeat(DocumentType type) { //获得所选文档类型节点的所有文档
        List<DocumentType> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE + "checkDocRepeat", type);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误checkDocRepeat~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getDoctypeSameLevel(CodeBase base) { //查看是否有同级节点
        List<DocumentType> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE + "getDoctypeSameLevel", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误getDoctypeSameLevel~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void updateDoctype(DocumentType base) {//更新doctype
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE + "updateDoctype", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误updateDoctype~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void updateDoctypeIsEnd(CodeBase base) {//更新doctype isEnd属性
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE + "updateDoctypeIsEnd", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误updateDoctypeIsEnd~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void insertDoctype(DocumentType base) {//插入doctype
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE + "insertDoctype", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误insertDoctype~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List checkDocChild(DocumentType type) { //查看是否有孩子节点
        List<DocumentType> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE + "checkDocChild", type);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误checkDocChild~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List checkDoctypeParent(DocumentType type) { //查看是否有父节点
        List<DocumentType> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE + "checkDoctypeParent", type);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误checkDoctypeParent~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void deleteDoctype(DocumentType dept) {//删除选中节点
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.delete(NAMESPACE + "deleteDoctype", dept);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE + "查询错误deleteDoctype~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void deleteDocument(int id) {
        //删除选中节点
        try {
            
            Map map = new HashMap();
            map.put("orgCode",LoginParam.getOrg().getCode());
            map.put("id",id);
            sqlmapclient.delete(NAMESPACE + "deleteDocument", map);
        } catch (SQLException ex) {
            Logger.getLogger(IbatisDocument.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
